from django.test import TestCase

# Create your tests here.
data = [
    {
        "id": 4,
        "name": "System",
        "code": "system",
        "component": "LAYOUT",
        "status": True,
        "icon": "ion:settings-outline",
        "title": "系统管理",
        "create_time": "2022-10-08 17:46:14",
        "type": 0,
        "children": [
            {
                "id": 5,
                "name": "UserList",
                "code": "user:list",
                "component": "/system/user/index",
                "status": True,
                "icon": "carbon:user-role",
                "title": "用户管理",
                "create_time": "2022-10-08 17:48:37",
                "type": 1
            },
            {
                "id": 8,
                "name": "DepartmentList",
                "code": "department:list",
                "component": "/system/department/index",
                "status": True,
                "icon": "carbon:user-role",
                "title": "部门管理",
                "create_time": "2022-10-08 17:48:37",
                "type": 1,
                "children": [
                    {
                        "id": 9,
                        "name": "DepartmentAddButton",
                        "code": "department:create",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:49:11",
                        "type": 2
                    },
                    {
                        "id": 10,
                        "name": "DepartmentUpdateButton",
                        "code": "department:update",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 11,
                        "name": "DepartmentDestroyButton",
                        "code": "department:destroy",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 12,
                        "name": "DepartmentExportButton",
                        "code": "department:export",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 13,
                        "name": "DepartmentImportButton",
                        "code": "department:import",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    }
                ]
            },
            {
                "id": 14,
                "name": "MenuList",
                "code": "permission:list",
                "component": "/system/menu/index",
                "status": True,
                "icon": "carbon:user-role",
                "title": "菜单管理",
                "create_time": "2022-10-08 17:48:37",
                "type": 1,
                "children": [
                    {
                        "id": 20,
                        "name": "MenuAddButton",
                        "code": "permission:create",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:49:11",
                        "type": 2
                    },
                    {
                        "id": 21,
                        "name": "MenuUpdateButton",
                        "code": "permission:update",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 22,
                        "name": "MenuDestroyButton",
                        "code": "permission:destroy",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 23,
                        "name": "MenuExportButton",
                        "code": "permission:export",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    },
                    {
                        "id": 24,
                        "name": "MenuImportButton",
                        "code": "permission:import",
                        "component": None,
                        "status": True,
                        "create_time": "2022-10-13 20:52:57",
                        "type": 2
                    }
                ]
            },
            {
                "id": 15,
                "name": "RoleList",
                "code": "role:list",
                "component": "/system/role/index",
                "status": True,
                "icon": "carbon:user-role",
                "title": "角色管理",
                "create_time": "2022-10-08 17:48:37",
                "type": 1
            }
        ]
    },
    {
        "id": 6,
        "name": "Resource",
        "code": "resource",
        "component": "LAYOUT",
        "status": True,
        "icon": "ion:tv-outline",
        "title": "资料管理",
        "create_time": "2022-10-08 17:46:14",
        "type": 0,
        "children": [
            {
                "id": 7,
                "name": "ResourceList",
                "code": "resource:list",
                "component": "/resource/resource/index",
                "status": True,
                "icon": "ion:tv-outline",
                "title": "资料管理列表",
                "create_time": "2022-10-08 17:48:37",
                "type": 1
            }
        ]
    }
]


def remove_button(tree_data):
    for index in range(len(tree_data) - 1, -1, -1):
        d = tree_data[index]
        if d.get('type') == 2:
            tree_data.pop(index)
        if d.get('children'):
            remove_button(d.get('children'))
    return tree_data


if __name__ == '__main__':
    print(remove_button(data))
